package cn.itcast.web.servlet;

import cn.itcast.domain.User;
import cn.itcast.service.UserService;
import cn.itcast.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1设置编码
        request.setCharacterEncoding("utf-8");
        //2获取数据
        //2.1获取用户填写验证码
        String verifycode = request.getParameter("verifycode");
        Map<String, String[]> map = request.getParameterMap();
        //验证验证码是否正确
        HttpSession session = request.getSession();
        String checkcode_server = (String)session.getAttribute("CHECKCODE_SERVER");
        //保证验证码的一次性

        if(!checkcode_server.equalsIgnoreCase(verifycode)){
            request.setAttribute("login_msg","验证码错误");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
            return;
        }
        session.removeAttribute("CHECKCODE_SERVER");
        //3封装User对象
        User user=new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //4调用service查询
        UserService service =new UserServiceImpl();
        User loginuser=service.login(user);
        //5判断是否登录成功
        if(loginuser!=null){
            //用户存入session，然后跳转页面
//            session.setAttribute("user",loginuser);
//            response.sendRedirect(request.getContextPath()+"/index.jsp");
            String sid = Integer.toString(loginuser.getId());
            char ch=sid.charAt(0);
            if(ch=='1'){
                request.setAttribute("user",loginuser);
                request.getRequestDispatcher("/hello1.jsp").forward(request,response);
            }else if(ch=='3'){
                request.setAttribute("user",loginuser);
                request.getRequestDispatcher("/hello3.jsp").forward(request,response);
            }else if(ch=='4'){
                request.setAttribute("user",loginuser);
                request.getRequestDispatcher("/hello4.jsp").forward(request,response);
            }else if(ch=='5'){
                request.setAttribute("user",loginuser);
                request.getRequestDispatcher("/hello5.jsp").forward(request,response);
            }else if(ch=='6'){
                request.setAttribute("user",loginuser);
                request.getRequestDispatcher("/hello6.jsp").forward(request,response);
            }else if(ch=='2'){
                request.setAttribute("user",loginuser);
                request.getRequestDispatcher("/hello2.jsp").forward(request,response);
            }else{
                request.getRequestDispatcher("/500.jsp").forward(request,response);
            }
        }
        else {
            request.setAttribute("login_msg","账号或密码错误！");
            request.getRequestDispatcher("/login.jsp").forward(request,response);

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
